package com.woniuxy.micromallserver.commoditymag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.bean.PageDto;
import com.woniuxy.micromallserver.bean.Commodity;
import com.woniuxy.micromallserver.commoditymag.mapper.CommodityMapper;
import com.woniuxy.micromallserver.commoditymag.service.CommodityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 17:29
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    @Resource
    public CommodityMapper commodityMapper;

    /**
     * 查询所有商品
     * @return
     */
    @Override
    public List<Commodity> getAllCommodities() {
        return commodityMapper.getAllCommodities();
    }


    /**
     * 根据商品id查询单个商品
     *
     * @param commodityId
     * @return
     */
    @Override
    public Commodity getCommodityById(int commodityId) {
        return commodityMapper.getCommodityById(commodityId);
    }

    /**
     * 根据商品状态查询商品
     *
     * @param commodityStatus
     * @return
     */
    @Override
    public List<Commodity> getCommoditiesByStatus(int commodityStatus) {
        return commodityMapper.getCommoditiesByStatus(commodityStatus);
    }

    /**
     * 多条件查询商品
     *
     * @return
     */
    @Override
    public List<Commodity> getCommoditiesByMultiConditions(Commodity commodity) {
        return commodityMapper.getCommoditiesByMultiConditions(commodity);
    }

    /**
     * 新增商品
     * @param commodity
     * @return int
     */
    @Override
    public int addCommodity(Commodity commodity) {
        return commodityMapper.addCommodity(commodity);
    }

    /**
     * 根据商品id上架、下架、删除商品，分别改变commodity_status为1、2、5
     *
     * @param commodityId
     * @param commodityStatus
     * @return int
     */
    @Override
    public int changeCommodityStatus(int commodityId, int commodityStatus) {
        return commodityMapper.changeCommodityStatus(commodityId, commodityStatus);
    }

    /**
     * 更新商品信息
     * @param commodity
     * @return
     */
    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityMapper.updateCommodity(commodity);
    }

    /**
     * 查询商品分页
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageDto findByPage(Integer pageNumber, Integer pageSize, Commodity commodity) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Commodity> data = commodityMapper.getCommoditiesByPage(commodity);
        PageInfo<Commodity> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setData(data);
        return pageDto;
    }
}
